/**
 * 
 */
package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.hanhai.zrb.api.biz.dao.mybatis.UserDynamicMapper;
import com.hanhai.zrb.api.biz.service.IndustryService;
import com.hanhai.zrb.api.biz.service.UserDynamicService;
import com.hanhai.zrb.api.biz.service.UserInfoService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.api.util.PageListUtil;
import com.hanhai.zrb.model.common.PageList;
import com.hanhai.zrb.model.project.Industry;
import com.hanhai.zrb.model.user.User;
import com.hanhai.zrb.model.user.enums.UserInfoName;
import com.hanhai.zrb.model.user.vo.UserInfoVO;
import com.hanhai.zrb.model.userDynamic.DynamicType;
import com.hanhai.zrb.model.userDynamic.ExtraInfo;
import com.hanhai.zrb.model.userDynamic.UserDynamic;
import com.hanhai.zrb.model.userDynamic.dto.UserDynamicDto;

/**
 * @author leiruiqi@hanhai.com
 *
 */
@Service("userDynamicService")
public class UserDynamicServiceImpl implements UserDynamicService {

	@Resource
	private UserDynamicMapper userDynamicMapper;
	
	@Resource
	private UserService userService;
	
	@Resource
	private UserInfoService userInfoService;
	
	@Resource
	private IndustryService industryService;
	
	@Override
	public List<UserDynamic> queryAllByUserId(Long userId) {
		
		return userDynamicMapper.queryAllByUserId(userId);
	}

	@Override
	public PageList queryPageByDto(UserDynamicDto dto, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize,true);
		List<UserDynamic> list = userDynamicMapper.queryByDto(dto);
		
		PageList pageList = PageListUtil.buildPageList(list);
		return pageList;
	}
	
	@Override
	public PageList queryPageNoCountByDto(UserDynamicDto dto, int pageNum,
			int pageSize) {
		PageHelper.startPage(pageNum, pageSize,false);
		List<UserDynamic> list = userDynamicMapper.queryByDto(dto);
		
		PageList pageList = PageListUtil.buildPageList(list);
		return pageList;
	}

	@Override
	public void createUserDynamic(UserDynamic userDynamic) {
		this.userDynamicMapper.create(userDynamic);
	}

	@Override
	public void createUserDynamic(Long userId, DynamicType type, String target,
			String title,ExtraInfo extraInfo) {
		UserDynamic userDynamic = new UserDynamic();
		userDynamic.setUserId(userId);
		userDynamic.setType(type.getCode());
		userDynamic.setTarget(target);
		userDynamic.setTitle(title);
		
		userDynamic.setExtraInfo(extraInfo);
		convertExtraObjectToStr(userDynamic);
		
		createUserDynamic(userDynamic);
	}
	
	@Override
	public void createUserDynamic(Long userId, DynamicType type, String target,
			String title) {
		UserDynamic userDynamic = new UserDynamic();
		userDynamic.setUserId(userId);
		userDynamic.setType(type.getCode());
		userDynamic.setTarget(target);
		userDynamic.setTitle(title);
		
		//获取需要的用户相关extra信息
		ExtraInfo extraInfo = getUserExtraInfo(userDynamic);
		userDynamic.setExtraInfo(extraInfo);
		
		//extraInfo对象转换成json格式
		convertExtraObjectToStr(userDynamic);
		
		//创建动态对象
		createUserDynamic(userDynamic);
	}
	
	private ExtraInfo getUserExtraInfo(UserDynamic userDynamic){
		User user = this.userService.findById(userDynamic.getUserId());
		if(user == null){
			return null;
		}
		
		ExtraInfo extraInfo = new ExtraInfo();
		
		extraInfo.setAvatar(user.getAvatar());
		extraInfo.setOrg(user.getOrg());
		extraInfo.setRealname(user.getRealname());
		
		Long industryId = user.getIndustryId();
		if(industryId !=null){
			Industry industry = industryService.selectByPrimaryKey(industryId.intValue());
			if(industry !=null ){
				extraInfo.setIndustryName(industry.getIndustryName());
			}
		}
		
		
		List<UserInfoName> queryNameList = new ArrayList<UserInfoName>();
		queryNameList.add(UserInfoName.POSITION);
		UserInfoVO userInfoVo = this.userInfoService.findByNameEnums(String.valueOf(user.getUid()), queryNameList);
		extraInfo.setPositionName(userInfoVo.getInfoMap().get(UserInfoName.POSITION.name()));
		
		return extraInfo;
	}

	@Override
	public void batchConverExtraDateForView(List<UserDynamic> list){
		for( UserDynamic userDynamic:list){
			convertExtraStrToObject(userDynamic);
			userDynamic.setExtra(null);
		}
	}
	
	private boolean convertExtraStrToObject(UserDynamic userDynamic){
		if(userDynamic.getExtra() == null){
			userDynamic.setExtraInfo(null);
			return true;
		}
		try {
			userDynamic.setExtraInfo(JSON.parseObject(userDynamic.getExtra(), ExtraInfo.class));
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}
	
	private boolean convertExtraObjectToStr(UserDynamic userDynamic){
		if(userDynamic.getExtraInfo() == null){
			userDynamic.setExtra(null);
			return true;
		}
		try {
			userDynamic.setExtra(JSON.toJSONString(userDynamic.getExtraInfo()));
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	public boolean deleteDynamic(List<Long> ids) {
		if( CollectionUtils.isEmpty(ids)){
			return false;
		}
		userDynamicMapper.deleteDynamic(ids);
		return true;
	}

}
